package com.juneng.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created on 2017/1/20.
 *
 * @author lubing
 *         com.zhongma.core
 *         15:48
 */
public class SecurityCheck {

    private final static String endFix = "appId=G0000111&key=3aeddbc9bfa7bbc22bf176118186a18f";

    /**
     * <ul>
     * <li>
     * signature method
     * </li>
     * </ul>
     *
     * @param map request params
     * @return signed string
     */
    public static String sinagture(Map<String, String> map) {
        Map<String, String> params = new TreeMap<>(
                Comparator.naturalOrder());

        // remove null value parameter
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                params.put(entry.getKey(), entry.getValue());
            }
        }
        System.out.println(params.toString());
        String s = params.toString().replace("{", "").replace("}", "").replace(", ", "&");

        // handle the last "&" charactor
        if (StringUtils.isNotBlank(s)) {
            s += "&" + endFix;
        } else {
            s += endFix;
        }

        return MD5Util.MD5(s);
    }
}
